package com.ns.scs.ads.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ns.core.current.config.SymbolsMark;
import com.ns.core.utils.date.CalendarPattern;
import com.ns.core.utils.date.CalenderUtil;
import com.ns.scs.ads.dto.AdsPageDto;
import com.ns.scs.ads.entry.AdsEntry;
import com.ns.scs.ads.mapper.AdsEntryMapper;
import com.ns.scs.ads.vo.AdsExtraVO;
import com.ns.scs.ads.vo.AdsVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 参数业务实现
 *
 * @author lw
 * @since 2024/09/23
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AdsServiceImpl extends ServiceImpl<AdsEntryMapper, AdsEntry> implements IAdsService {
    private final AdsEntryMapper mapper;

    @Override
    public Page<AdsExtraVO> getList(AdsPageDto dto) {
        return mapper.getList(dto, new Page<>(dto.getCurrent(), dto.getSize()));
    }

    @Override
    public long getMaxSort() {
        return mapper.getMaxSort();
    }

    @Override
    public Map<String, AdsVO> getByServerCode(String code) {
        Map<String, AdsVO> map = new HashMap<>();
        List<AdsVO> adsList = mapper.getByServerCode(code);
        if (CollectionUtils.isEmpty(adsList)) {
            return map;
        }
        return adsList.stream().collect(Collectors.toMap(AdsVO::getSpaceCode, adsVO -> adsVO, (a, b) -> b));
    }

    @Override
    public void syncExpiration() {
        LambdaQueryWrapper<AdsEntry> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(AdsEntry::getExpiration, SymbolsMark.TWO)
                .eq(AdsEntry::getAuditStatus, SymbolsMark.ONE)
                .eq(AdsEntry::getStatus, SymbolsMark.ONE)
                .eq(AdsEntry::getDeleted, SymbolsMark.ZERO);
        List<AdsEntry> adsEntryList = mapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(adsEntryList)) {
            return;
        }
        adsEntryList.forEach(adsEntry -> {
            String startDate = adsEntry.getStartDate();
            String endDate = adsEntry.getEndDate();
            String nowYmd = CalenderUtil.getNowString(CalendarPattern.Y_M_D);

            // 比较当前日期与开始日期和结束日期的大小
            if (nowYmd.compareTo(startDate) < SymbolsMark.ZERO) {
                // 如果当前日期小于开始日期，将状态设置为0
                adsEntry.setExpiration(SymbolsMark.ZERO);
            } else if (nowYmd.compareTo(endDate) > SymbolsMark.ZERO) {
                // 如果当前日期大于结束日期，将状态设置为2
                adsEntry.setExpiration(SymbolsMark.TWO);
            } else {
                // 否则，将状态设置为1
                adsEntry.setExpiration(SymbolsMark.ONE);
            }
        });
        mapper.updateById(adsEntryList);
    }
}
